package com.andatsoft.laisim.dialog;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.adapter.SkinListDialogAdapter;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.entity.SkinItem;
import com.andatsoft.laisim.service.PlayerService;
import com.andatsoft.laisim.theme.ThemeManager;
import com.andatsoft.laisim.utils.ToastMessage;
import com.andatsoft.laisim.view.WPProgressBar;

public class SkinChooserDialog extends BaseActivity {

	// public static final String BROADCAST_THEME_CHANGED =
	// "com.laisim.theme.changed";
	private List<SkinItem> mSkinList;

	private ListView mListView;
	private TextView mTvOk;
	private int mSkinSelectedId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(
				new ColorDrawable(getResources().getColor(R.color.black)));
		setContentView(R.layout.dialog_skin_chooser);

		loadAllSkin();
		mListView = (ListView) findViewById(R.id.list_skin_dialog);
		mListView.setAdapter(new SkinListDialogAdapter(getApplicationContext(),
				mSkinList));
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {

				mSkinSelectedId = mSkinList.get(pos).id;
				setSelectedSkin(mSkinSelectedId);
				// // ThemeManager.loadTheme(getApplicationContext());
				// // sendBroadcast(new Intent(BROADCAST_THEME_CHANGED));
				// // ThemeChooserDialog.this.finish();
				// mThemeList.get(pos).isSelected = 1;
			}
		});
		mTvOk = (TextView) findViewById(R.id.tv_skin_chooser_dialog_ok);
		mTvOk.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(mTvOk, 0.5f);
					return true;
				case MotionEvent.ACTION_UP:
					setAlpha(mTvOk, 1.0f);
					if (mSkinSelectedId != -1) {
						new SkinChangedTask().execute();
					} else {
						SkinChooserDialog.this.finish();
					}
					return true;
				}
				return false;
			}
		});

	}

	private void loadAllSkin() {
		DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
		mSkinList = db.getAllSkin();
		if (mSkinList == null || mSkinList.size() <= 0) {
			ThemeManager.importBuiltInSkin(db);
			mSkinList = db.getAllSkin();
		}
	}

	private void setSelectedSkin(int id) {
		for (SkinItem si : mSkinList) {
			if (id == si.id) {
				si.isSelected = 1;
			} else {
				si.isSelected = 0;
			}
		}
		((SkinListDialogAdapter) mListView.getAdapter()).notifyDataSetChanged();
	}

	class SkinChangedTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			((WPProgressBar) findViewById(R.id.progress_skin_chooser_dialog))
					.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... params) {
			// changed skin selected in db
			DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
			db.setSelectedSkin(mSkinSelectedId);
			// release all rsource
			// ThemeManager.releaseAll();
			// load skin
			ThemeManager.loadSkin(getApplicationContext(),
					ThemeManager.MAIN_ACTIVYTY);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			((WPProgressBar) findViewById(R.id.progress_skin_chooser_dialog))
					.setVisibility(View.GONE);
			SkinChooserDialog.this.finish();
			sendBroadcast(new Intent(PlayerService.INTENT_SKIN_CHANGED));
			new ToastMessage(getApplicationContext())
					.showShortMessage(getString(R.string.toast_skin_applied));
		}

	}

}
